// eslint-disable-next-line spaced-comment

import vue from '@vitejs/plugin-vue';
import vueJsxPlugin from '@vitejs/plugin-vue-jsx';
import * as path from 'path';
import visualizer from 'rollup-plugin-visualizer';
import unocss from 'unocss/vite';
import AutoImport from 'unplugin-auto-import/vite';
import ElementPlus from 'unplugin-element-plus/vite';
import { defineConfig } from 'vite';
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

const { TYPE } = process.env;

// https://vitejs.dev/config/
export default ({ command }) => {
  const env = command === 'serve' ? 'development' : 'production';
  return defineConfig({
    define: {
      'process.env.NODE_ENV': JSON.stringify(env),
      'import.meta.env.TYPE': JSON.stringify(TYPE)
    },
    base: command === 'serve' ? '' : '/decision-engine/',
    server: {
      port: 8888,
      hmr: true,
      proxy: {
        '/api': {
          target: 'https://dev3.mcisaas.com',
          secure: false,
          changeOrigin: true
        }
      }
    },
    plugins: [
      vue(),
      unocss(),
      visualizer({
        open: false,
        gzipSize: true,
        brotliSize: true
      }),
      ElementPlus(),
      vueJsxPlugin(),
      // 自动导入 vue、vue-router api、hooks 引用
      AutoImport({
        imports: ['vue', 'vue-router', 'vue-i18n'],
        dts: 'src/@types/auto-import.d.ts'
      }),
      // 按需引入 element-plus 组件
      // vite-plugin-style-import -> 2.0.0
      // element-plus -> 2.0.4
      createStyleImportPlugin({
        resolves: [ElementPlusResolve()]
      }),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]'
      }),
      {
        org: 'agx666',
        project: 'decision-engine-web',
        telemetry: false,
        // Auth tokens can be obtained from https://sentry.io/settings/account/api/auth-tokens/
        // and need `project:releases` and `org:read` scopes
        authToken: 'cfbb096b73e1d2e7430d8af04d08e853ac1897b4c06585a9f24bf01fd842a285',
        sourcemaps: {
          ignore: ['node_modules']
        },
        errorHandler: (err) => {
          console.warn(err);
        }
      }
    ],
    // test: {
    //   globals: true,
    //   environment: 'jsdom',
    //   transformMode: {
    //     web: [/.[tj]sx$/, /.vue/]
    //   },
    //   exclude: ['node_modules'],
    //   deps: {
    //     inline: ['element-plus']
    //   }
    // },
    build: {
      rollupOptions: {
        input: 'index.html',
        output: {
          manualChunks(id, { getModuleInfo }) {
            const moduleInfo = getModuleInfo(id);
            const importersLen = moduleInfo.importers.length;
            if (id.includes('node_modules')) {
              if (importersLen > 2) {
                return 'vendor';
              }
            }
            return null;
          }
        }
      }
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@public': path.resolve(__dirname, 'public')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use './src/styles/common.scss' as * ;`
        }
      }
    }
  });
};
